import 'package:flutter/material.dart';

/**
 *  表单界面
 */
class FormDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Theme(
//        data: ThemeData(
//          //替代main里面的主题
//          primaryColor: Colors.black,
//        ),
        //换成特定的主题
        data: Theme.of(context).copyWith(
          primaryColor: Colors.black,
        ),
        child: Container(
          //内边距
          padding: EdgeInsets.all(16.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
//              TextFielDemo(),
              RegistForm(),
            ],
          ),
        ),
      ),
    );
  }
}

class RegistForm extends StatefulWidget {
  RegistFormState createState() => RegistFormState();
}

class RegistFormState extends State<RegistForm> {
  final _registFormKey = GlobalKey<FormState>();

  String _userName, _password;

  var _mAutovalidate = false;

  void _submit() {
    if (_registFormKey.currentState.validate()) {
      _registFormKey.currentState.save();
      debugPrint('UserName: $_userName,Password: $_password');
      Scaffold.of(context).showSnackBar(SnackBar(
        content: Text('Regist....'),
      ));
    } else {
      setState(() {
        _mAutovalidate = true;
      });
    }
  }

  /**
   * 验证用户名
   */
  String _validatorUserName(value) {
    if (value == null || value.isEmpty) {
      return 'UserName is null';
    }
    return null;
  }

  /**
   * 验证密码
   */
  String _validatorPassword(value) {
    if (value == null || value.isEmpty) {
      return 'Password is null';
    }
    return null;
  }

  @override
  Widget build(BuildContext context) {
    return Form(
      key: _registFormKey,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          TextFormField(
            decoration: InputDecoration(
              labelText: 'UserName',
              helperText: '',
            ),
            onSaved: (value) {
              _userName = value;
            },
            validator: _validatorUserName,
            //自动验证
            autovalidate: _mAutovalidate,
          ),
          TextFormField(
            //敏感数据
            obscureText: true,
            decoration: InputDecoration(
              labelText: 'Password',
              helperText: '',
            ),
            onSaved: (value) {
              _password = value;
            },
            validator: _validatorPassword,
            //自动验证
            autovalidate: _mAutovalidate,
          ),
          SizedBox(
            height: 32.0,
          ),
          Container(
            width: double.infinity,
            child: RaisedButton(
              color: Colors.blue, //Theme.of(context).accentColor
              child: Text(
                'Regist',
                style: TextStyle(
                  color: Colors.white,
                ),
              ),
              elevation: 0.0,
              onPressed: () {
                _submit();
              },
            ),
          ),
        ],
      ),
    );
  }
}

/**
 * 文本控件
 */
class TextFielDemo extends StatefulWidget {
  TextFielDemoState createState() => TextFielDemoState();
}

class TextFielDemoState extends State<TextFielDemo> {
  final _textEditingController = TextEditingController();

  @override
  void dispose() {
    super.dispose();
    _textEditingController.dispose();
  }

  @override
  void initState() {
    super.initState();
    //初始数据
//    _textEditingController.text = 'Hi';
    _textEditingController.addListener(() {
      debugPrint('input is ${_textEditingController.text}');
    });
  }

  @override
  Widget build(BuildContext context) {
    return TextField(
      //设置controller
      controller: _textEditingController,
      //设置监听
//      onChanged: (value) {
//        debugPrint('input value is $value');
//      },
      //按下确定按钮获取文本的值
      onSubmitted: (value) {
        debugPrint('submit value is $value');
      },
      decoration: InputDecoration(
        icon: Icon(Icons.subject),
        labelText: '用户名',
        hintText: '请输入用户名',
        //不设置边框
        //border: InputBorder.none,
        //设置边框
        //border: OutlineInputBorder(),
        //设置背景颜色为灰色
        filled: true,
      ),
    );
  }
}

/**
 * 主题
 */
class ThemeDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Theme.of(context).accentColor,
    );
  }
}
